package xwj.example.demo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import xwj.example.demo.domain.Supplier;
import xwj.example.demo.exception.ProductAdminException;
import xwj.example.demo.service.ISupplierService;
import xwj.example.demo.service.impl.SupplierServiceImpl;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xwj
 * @since 2022-10-30
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    SupplierServiceImpl supplierService;


    @GetMapping("/{id}")
    public ResponseEntity<Supplier> getSupplier(@PathVariable long id){
        Supplier result = supplierService.getSupplier(id);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }

    @GetMapping("")
    public IPage<Supplier> findSupplier(String name, @RequestParam(defaultValue = "0")Integer pageNum,
                                         @RequestParam(defaultValue = "10")Integer pageSize){
        return supplierService.selectSupplierPage(name, pageNum, pageSize);
    }

    /**
     * 添加供应商
     * @param supplier
     * @return
     * @throws ProductAdminException 在Controller抛出异常，可以通过全局异常处理进行捕获
     */

    @PostMapping("")
    public Supplier addSupplier(@RequestBody Supplier supplier) throws ProductAdminException {
        supplierService.addSupplier(supplier);
        return supplier;
    }


    @PutMapping("/{id}")
    public void updateSupplier(@PathVariable long id,@RequestBody Supplier supplier) throws ProductAdminException {
        supplierService.updateSupplier(id,supplier);
    }


    @DeleteMapping("/{id}")
    public void deleteSupplier(@PathVariable long id){
        supplierService.deleteSupplier(id);
    }

}

